package cn.rongdao.jrkabao.ui.loan.adapter;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextPaint;
import android.view.View;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.loan.CreditCardLoansDateBean;
import cn.rongdao.jrkabao.ui.information.adapter.baseadapter.BaseRecycleAdapter;
import cn.rongdao.jrkabao.ui.information.adapter.baseadapter.BaseViewHolder;

/**
 * Created by Wang on 17/9/18.
 */

public class CreditLoanTabAdapter extends BaseRecycleAdapter<CreditCardLoansDateBean.StagesBean.PeriodBean> {
    TextView copy_view;
    private int lastSelectedItem = -1;
    private int maxValue =0;
    private Map<Integer, TextView> textViewMap = new ArrayMap<>();
    private Context context;

    public CreditLoanTabAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_credit_cardloans_date;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void convert(BaseViewHolder holder, CreditCardLoansDateBean.StagesBean.PeriodBean bean, int position) {
        TextView view = (TextView) holder.getView(R.id.item_creditcard_tv);
        view.setText(bean.getDesc());
        textViewMap.put(position,view);
        if(bean.isSelected()){
            TextPaint tp = view.getPaint();
            tp.setFakeBoldText(true);
            view.setTextColor(context.getResources().getColor(R.color.term_selected));
            view.setBackgroundResource(R.drawable.whitebg_100radius);
        }else {
            TextPaint tp = view.getPaint();
            tp.setFakeBoldText(false);
            view.setTextColor(context.getResources().getColor(R.color.term_unSelected));
            view.setBackgroundResource(R.drawable.whitebg_100_border_1);
        }
    }

    public void setCopy_view(TextView copy_view) {
        this.copy_view = copy_view;
    }
    /*暴力清除之前的项*/
    public void resetTage(){
        List<CreditCardLoansDateBean.StagesBean.PeriodBean> dataList = getDataList();
        if(dataList.size() >0){
            for (int i = 0; i < dataList.size(); i++) {
                CreditCardLoansDateBean.StagesBean.PeriodBean creditCardLoansDateBean = dataList.get(i);
                if(creditCardLoansDateBean.isSelected()) {
                    String val = creditCardLoansDateBean.getVal();
                    creditCardLoansDateBean.setSelected(false);
                }
            }
        }
        notifyDataSetChanged();
    }

    /*设置选中的项*/
    public View setItemTage(int position){

        lastSelectedItem = position;
        if(copy_view != null){
            copy_view.setText("");
        }
        List<CreditCardLoansDateBean.StagesBean.PeriodBean> dataList = getDataList();
        resetTage();
        if(dataList.size() >0){
            if(position < dataList.size()) {
                CreditCardLoansDateBean.StagesBean.PeriodBean periodBean = dataList.get(position);
                periodBean.setSelected(true);
                String desc = periodBean.getDesc();
                copy_view.setText(desc);
            }
        }
        notifyDataSetChanged();
         if(textViewMap.size() >0 && position < textViewMap.size()) {
             TextView textView = textViewMap.get(position);
             return  textView;
         }
         return  null;
    }

    /*外部调用 点击选中*/
    public CreditCardLoansDateBean.StagesBean.PeriodBean getItemDataBean(int position){
        List<CreditCardLoansDateBean.StagesBean.PeriodBean> dataList = getDataList();
        if(dataList!= null && dataList.size() >0){
            CreditCardLoansDateBean.StagesBean.PeriodBean periodBean = dataList.get(position);
            return periodBean;
        }
        return  null;
    }

    /*获取最后选中的那一项*/
    public String getLastSelectedItem() {
        if(lastSelectedItem != -1) {
            if(lastSelectedItem < getDataList().size()) {
                String val = getDataList().get(lastSelectedItem).getVal();
                return val;
            }
        }
        return "";
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    /*外部调用 item是否被选中*/
    public CreditCardLoansDateBean.StagesBean.PeriodBean getItemSelectedBean(){
        boolean tage = false;
        List<CreditCardLoansDateBean.StagesBean.PeriodBean> dataList = getDataList();
        if(dataList!= null && dataList.size() >0 ) {
            for (int i = 0; i < dataList.size(); i++) {
                CreditCardLoansDateBean.StagesBean.PeriodBean periodBean = dataList.get(i);
                boolean selected = periodBean.isSelected();
                if(selected){
                    return periodBean;
                }
            }
        }
        return  null;
    }
}
